//
//  ViewController.m
//  DMLTableView
//
//  Created by 戴明亮 on 2017/9/9.
//  Copyright © 2017年 戴明亮. All rights reserved.
//

#import "ViewController.h"
#import "DMLParallaxHeader.h"
#import "DMLScrollView.h"
#import "ZXRomSettleCategoryManage.h"
#import "TempViewController.h"
#import "DMLHeaderView.h"

#define SPANISH_WHITE [UIColor colorWithRed:0.996 green:0.992 blue:0.941 alpha:1]
@interface ViewController ()<DMLScrollViewDelegate,UIScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,ZXRomSettleCategoryManageDelegate>
@property (nonatomic, strong) DMLScrollView *scrollView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) ZXRomSettleCategoryManage *SettleCategoryManage;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.collectionView];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 初始化控件
    DMLHeaderView *view =   [NSBundle.mainBundle loadNibNamed:@"DMLHeaderView" owner:self options:nil].firstObject;
    
    ZXRomSettleCategoryManage *SettleCategoryManage = [[ZXRomSettleCategoryManage alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40) WithSelectIndex:0];
    SettleCategoryManage.delegate =self;
    self.SettleCategoryManage = SettleCategoryManage;
    SettleCategoryManage.isScrollEnable = YES;
    [view.settleCategoryView addSubview:SettleCategoryManage];
    
    NSMutableArray *arrayMTitle  = [NSMutableArray arrayWithObjects:@"好友",@"群",@"多人群聊",@"设备",@"通讯录", @"公众号",nil];
    self.SettleCategoryManage.arrayTitle = arrayMTitle;
    
    self.scrollView.parallaxHeader.view = view;
    self.scrollView.parallaxHeader.height = 153;
    
    self.scrollView.parallaxHeader.minimumHeight = 35;
    
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIndentifier"];
    
    
    // 注意在这里可以设置不同的控制器 我这边写的是一个控制器 建议你分开写多个控制器
    for (int i=0; i< 6; i++) {
        TempViewController *vc = [[TempViewController alloc]init];
        [self addChildViewController:vc];
        
    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    CGRect frame = self.view.frame;
    frame.origin.y += 64;
    
    self.scrollView.frame = frame;
    self.scrollView.contentSize = frame.size;
    
    
    
    frame.size.height -= self.scrollView.parallaxHeader.minimumHeight;
    self.collectionView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height - 64 );
    
    
}

- (DMLScrollView *)scrollView {
    if(!_scrollView) {
        _scrollView = [[DMLScrollView alloc] init];
        _scrollView.delegate = self;
    }
    return _scrollView;
}

- (UICollectionView *)collectionView
{
    if (_collectionView == nil) {
        
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - self.scrollView.parallaxHeader.minimumHeight);
        layout.minimumLineSpacing =0;
        layout.minimumInteritemSpacing =0;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width -self.scrollView.parallaxHeader.minimumHeight) collectionViewLayout:layout];
        
        _collectionView.backgroundColor = [UIColor yellowColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        
    }
    return _collectionView;
}

#pragma mark  拖动偏移滑动视图时候调用的方法
//滑动结束的时候调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    
    if (scrollView==self.collectionView) {
        
        //根据偏移量  发送通知刷新数据
        int page = scrollView.contentOffset.x/self.view.frame.size.width;
        
        self.SettleCategoryManage.currentPage = page + 1;
        NSLog(@"->>>>>>>----------->%d",page);
        
    }
    
}

#pragma mark -- 顶部视图控制按钮的代理方法
- (void)ZXRomSettleCategoryManage:(ZXRomSettleCategoryManage *)settleCategoryManage WithButton:(UIButton *)button
{
    NSLog(@"--->%@",button.titleLabel.text);
    
    self.collectionView.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width *(button.tag - 1), 0);
    
    
    
}


#pragma mark -- UICollectionViewDelegate

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  self.childViewControllers.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIndentifier" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    //添加新的
    TempViewController *vc = [self.childViewControllers objectAtIndex:indexPath.item];
    
    
    
    vc.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 55);
    
    
    [cell.contentView addSubview:vc.view];
    
    
    NSLog(@"cell的子视图 -- %@ 控制器的子控制器 -- %@",cell.subviews,self.childViewControllers);
    
    
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
